<?php

class ImagesController extends FormController
{

  protected $table = 'forms_images';

  protected $pk = 'image_id';

  protected $orderField = 'image_order';

  protected $sortable = true;

  protected function initialize()
  {
    parent::initialize();

    $this->Form->addElement(new FormElement_Image('image_filename', 'Image'), Form::ACTION_CRUD | Form::ACTION_IMAGES);
    $this->Form->addElement(new FormElement_Text('image_caption', 'Caption'), Form::ACTION_CRUD | Form::ACTION_IMAGES);

    $filter0 = new FormFilter_Select('image_form_id', 'Form');
    $filter0->table = 'forms';
    $filter0->keyField = 'form_id';
    $filter0->labelField = 'form_text';
    $filter0->visible = false;
    $filter0->required = true;
    $filter0->redirect = '/admin/forms';

    $this->Form->addFilter($filter0);
  }

  protected function indexAction()
  {
    try {
      $this->Form->execute(Form::ACTION_IMAGES);
    }
    catch (FormNoSuchActionException $e) {
      $this->response->redirect('/admin');
    }
    catch (ValidationException $e) {
      s::getInstance()->warnings($e->getErrors());
    }

    return $this->Form->render(Form::ACTION_IMAGES);
  }

}
